﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Alerts_Administration_2010.interfaces.views;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Alerts_Administration_2010.classes.views
{
    public class AlertDetailsView : IAlertDetailsView
    {
        #region private fields
        private TextBox _title = new TextBox();
        private Label _emailAddress = new Label();
        private RadioButtonList _changeType = new RadioButtonList();
        private RadioButtonList _filter = new RadioButtonList();
        private RadioButtonList _frequency = new RadioButtonList();
        private DropDownList _alertTimeDay = new DropDownList();
        private DropDownList _alertTimeHour = new DropDownList();
        private bool _changeTypeVisible;
        private bool _dayEnabled;
        private bool _hourEnabled;
        private Control _container;
        #endregion

        #region public properties
        public string AlertTitle
        {
            get
            {
                return _title.Text;
            }
            set
            {
                _title.Text = value;
            }
        }
        public string EmailAddress
        {
            get
            {
                return _emailAddress.Text;
            }
            set
            {
                _emailAddress.Text = value;
            }
        }
        public string ChangeType
        {
            get
            {
                return _changeType.SelectedValue;
            }
            set
            {
                foreach (ListItem li in _changeType.Items)
                {
                    if (li.Value == value)
                    {
                        li.Selected = true;
                    }
                    else
                    {
                        li.Selected = false;
                    }
                }
            }
        }
        public string Filter
        {
            get
            {
                return _filter.SelectedValue;
            }
            set
            {
                foreach (ListItem li in _filter.Items)
                {
                    if (li.Value == value)
                    {
                        li.Selected = true;
                    }
                    else
                    {
                        li.Selected = false;
                    }
                }
            }
        }
        public string Frequency
        {
            get
            {
                return _frequency.SelectedValue;
            }
            set
            {
                foreach (ListItem li in _frequency.Items)
                {
                    if (li.Value == value)
                    {
                        li.Selected = true;
                    }
                    else
                    {
                        li.Selected = false;
                    }
                }
            }
        }
        public string AlertDay
        {
            get
            {
                return _alertTimeDay.SelectedValue;
            }
            set
            {
                foreach (ListItem li in _alertTimeDay.Items)
                {
                    if (li.Value == value)
                    {
                        li.Selected = true;
                    }
                    else
                    {
                        li.Selected = false;
                    }
                }
            }
        }
        public string AlertHour
        {
            get
            {
                return _alertTimeHour.SelectedValue;
            }
            set
            {
                foreach (ListItem li in _alertTimeHour.Items)
                {
                    if (li.Value == value)
                    {
                        li.Selected = true;
                    }
                    else
                    {
                        li.Selected = false;
                    }
                }
            }
        }
        public bool ChangeTypeVisible
        {
            set { _changeTypeVisible = value; }
        }
        public bool DayEnabled
        {
            set
            {
                _dayEnabled = value;
                _alertTimeDay.Enabled = value;
            }
        }
        public bool HourEnabled
        {
            set
            {
                _hourEnabled = value;
                _alertTimeHour.Enabled = value;
            }
        }
        #endregion

        #region public events
        public event EventHandler<EventArgs> ChangeTypeChanged;
        public event EventHandler<EventArgs> Canceled;
        public event EventHandler<EventArgs> Confirmed;
        #endregion

        public AlertDetailsView(Control container)
        {
            _container = container;

            #region set control's IDs
            _title.ID = "AlertTitle";
            _emailAddress.ID = "EmailAddress";
            _changeType.ID = "ChangeType";
            _filter.ID = "Filter";
            _frequency.ID = "Frequency";
            _alertTimeDay.ID = "AlertTimeDay";
            _alertTimeHour.ID = "AlertTimeHour";
            #endregion

            #region initialize Frequency
            _frequency.AutoPostBack = true;
            _frequency.SelectedIndexChanged += new EventHandler(_frequency_SelectedIndexChanged);
            #endregion

            #region initialize AlertTime
            for (DateTime dt = new DateTime(2010, 9, 19); dt < new DateTime(2010, 9, 26); dt = dt.AddDays(1))
            {
                _alertTimeDay.Items.Add(new ListItem(dt.DayOfWeek.ToString(), dt.DayOfWeek.ToString()));
            }

            for (int i = 0; i < 24; i++)
            {
                _alertTimeHour.Items.Add(new ListItem(((i + 11) % 12 + 1).ToString() + ":00 " + ((i / 12) == 0 ? "AM" : "PM"), i.ToString()));
            }
            #endregion

            _title.Width = 250;
        }

        #region public methods
        public void SetFilters(IEnumerable<string> filters)
        {
            foreach (string filter in filters)
            {
                _filter.Items.Add(new ListItem(filter, filter));
            }
        }

        public void SetChangeTypes(IEnumerable<string> changeTypes)
        {
            foreach (string changeType in changeTypes)
            {
                _changeType.Items.Add(new ListItem(changeType, changeType));
            }
        }

        public void SetFrequencies(IEnumerable<string> frequencies)
        {
            foreach (string frequency in frequencies)
            {
                _frequency.Items.Add(new ListItem(frequency, frequency));
            }
        }

        public object Render()
        {
            Panel detailsView = new Panel();

            Table tableUpdate = new Table();
            AddRow(tableUpdate, "Alert title", _title);
            AddRow(tableUpdate, "Email", _emailAddress);
            if (_changeTypeVisible) AddRow(tableUpdate, "Change Type (Event Type)", _changeType);
            AddRow(tableUpdate, "Filter", _filter);
            AddRow(tableUpdate, "Frequency", _frequency);
            Panel p = new Panel();
            p.Controls.Add(_alertTimeDay);
            p.Controls.Add(_alertTimeHour);
            AddRow(tableUpdate, "AlertTime", p);

            Button confirm = new Button();
            confirm.ID = "confirm";
            confirm.Text = "Submit changes";
            confirm.Click += new EventHandler(confirm_Click);

            Button cancel = new Button();
            cancel.ID = "cancel";
            cancel.Text = "Cancel";
            cancel.Click += new EventHandler(cancel_Click);

            detailsView.Controls.Add(tableUpdate);
            detailsView.Controls.Add(confirm);
            detailsView.Controls.Add(cancel);

            _container.Controls.Clear();
            _container.Controls.Add(detailsView);

            return detailsView;
        }
        #endregion

        #region event handler
        private void cancel_Click(object sender, EventArgs e)
        {
            if (Canceled != null) Canceled(this, e);
        }

        private void confirm_Click(object sender, EventArgs e)
        {
            if (Confirmed != null) Confirmed(this, e);
        }

        private void _frequency_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ChangeTypeChanged != null) ChangeTypeChanged(this, e);
        }
        #endregion

        private static void AddRow(Table t, string description, Control control)
        {
            TableRow tr = new TableRow();

            t.Rows.Add(tr);

            TableCell td1 = new TableCell();
            TableCell td2 = new TableCell();

            tr.Controls.Add(td1);
            tr.Controls.Add(td2);

            Label label = new Label();
            label.Text = description;

            td1.Controls.Add(label);
            td2.Controls.Add(control);
        }
    }
}
